package Server;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import utils.*;

public class ServerGUI extends JFrame implements Observer{

	
	public static Color DEFAULT_BG_COLOR = new Color(17, 178, 233);
	public static Map<Integer, String> playerStates = new HashMap<Integer, String>();
	
	static{
		playerStates.put(new Integer(Player.STATE_NONE), "None");
		playerStates.put(new Integer(Player.STATE_CONNECTED), "Connected");
		playerStates.put(new Integer(Player.STATE_PLAYING), "Playing");
		playerStates.put(new Integer(Player.STATE_WAITING), "Waiting");		
	}
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -9107242202897205055L;
	private Server server;
	private int height;
	private int width;
	private Container cp;
	
	private JButton stopServerBtn, startServerBtn;
	
	private JPanel playersPanel, 
				   gamesPanel, 
				   invitationsPanel, 
				   commandPanel, 
				   logoPanel,
				   logPanel,
				   centerPanel,
				   topPanel;
	
	
	private JTextArea logTextArea;
	private JScrollPane scrollPane;
	
	
	
	public ServerGUI(Server srv, int width, int height){
		
		super("NavalBattle server");
		
		this.server = srv;
		this.width = width;
		this.height = height;
		this.cp = this.getContentPane();
				
		server.addObserver(this);
				
		this.setPreferredSize(new Dimension(width, height));
		this.setLocation(200, 100);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.pack();
		this.setVisible(true);
		setResizable(false);
		
		setView();
		addListeners();
		
		this.addComponentListener(new ComponentAdapter() {
			@Override
			public void componentMoved(ComponentEvent arg0) {
				revalidate();
				repaint();
			}
		});
		
		update(null, this);
	}

	
	private void setView(){

		cp.setBackground(DEFAULT_BG_COLOR);
		cp.setMinimumSize(new Dimension(width, height));
		this.setMinimumSize(new Dimension(width,height));
		
		playersPanel = new JPanel();
		gamesPanel = new JPanel();
		invitationsPanel = new JPanel();
		commandPanel = new JPanel();
		logoPanel = new JPanel();
		logPanel = new JPanel();
		centerPanel = new JPanel();
		topPanel = new JPanel();
		
		cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));
		topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
		
		//centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.X_AXIS));
		centerPanel.setPreferredSize(new Dimension(width, 400));
		
		cp.add(topPanel);
		cp.add(centerPanel);
		cp.add(logPanel);
		
		topPanel.add(logoPanel);
		topPanel.add(commandPanel);
		
		centerPanel.add(playersPanel);
		centerPanel.add(gamesPanel);
		centerPanel.add(invitationsPanel);
		
		
		/*----------------------------------*/
		
		logPanel.setPreferredSize(new Dimension(this.width, 140));
		logPanel.setBackground(DEFAULT_BG_COLOR);
		
		//In a container that uses a BorderLayout:
		logTextArea = new JTextArea(6, 40);
		logTextArea.setBackground(DEFAULT_BG_COLOR);
		logTextArea.setEditable(false);
		logTextArea.addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent arg0) {
				revalidate();
				repaint();
			}
		});
	
		logTextArea.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				//revalidate();
				//repaint();
			}
					
		});
		
		scrollPane = new JScrollPane(logTextArea);
		scrollPane.setBackground(new Color(0,0,0,0));
		scrollPane.setBorder(BorderFactory.createEmptyBorder());
		scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
			
			@Override
			public void adjustmentValueChanged(AdjustmentEvent e) {
				revalidate();
				repaint();				
			}
		});
		
		
		//setPreferredSize(new Dimension(450, 110));
		
		add(scrollPane);
		
		topPanel.setBackground(new Color(0,0,0,0));
		topPanel.setPreferredSize(new Dimension(this.width, 120));
	
		centerPanel.setBackground(DEFAULT_BG_COLOR);
		centerPanel.setLayout(null);
		
		logoPanel.setBackground(DEFAULT_BG_COLOR);
		logoPanel.add(new ImagePanel("C:\\Users\\Rodrigo\\Dropbox\\Trabalhos\\PDist\\Partilha PDist\\LogoServer.png", 0));
		
		playersPanel.setLayout(new BoxLayout(playersPanel, BoxLayout.Y_AXIS));
		playersPanel.setBackground(new Color(0,0,0,0));
		playersPanel.setBounds(0, 0, width/3, 300);
		playersPanel.add(new JLabel("Players"));
		
		
		gamesPanel.setLayout(new BoxLayout(gamesPanel, BoxLayout.Y_AXIS));
		gamesPanel.setBackground(DEFAULT_BG_COLOR);
		gamesPanel.setBounds(width/3, 0, width/3, 300);
		gamesPanel.add(new JLabel("::: Games :::"));
		
		invitationsPanel.setLayout(new BoxLayout(invitationsPanel, BoxLayout.Y_AXIS));
		invitationsPanel.setBackground(new Color(0,0,0,0));
		invitationsPanel.setBounds(width*2/3, 0, width/3, 300);
		invitationsPanel.add(new JLabel("::: Invitations :::"));
	
		stopServerBtn = new JButton("Stop Server");
		startServerBtn = new JButton("Start Server");
		
		commandPanel.setBackground(DEFAULT_BG_COLOR);
		commandPanel.add(stopServerBtn);
		commandPanel.add(startServerBtn);
		
	
		
	}
	
	private void addListeners(){
	
		startServerBtn.addMouseListener(new MouseAdapter() {
		@Override
		public void mousePressed(MouseEvent e) {
			server.startServer();
		}});
		
		stopServerBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				server.shutdown();
			}});
	}
	
	
	
	@Override
	public void update(Observable o, Object arg) {
		
		
		/*Update Buttons*/
		
		if (server.isRunning()){
			startServerBtn.setEnabled(false);
			stopServerBtn.setEnabled(true);
		}
		else{
			startServerBtn.setEnabled(true);
			stopServerBtn.setEnabled(false);
		}
			
		
		
		/*--Update Players List--*/
		
		playersPanel.removeAll();
		
		playersPanel.add(new JLabel("::: PLAYERS ::::"));
		
		for (Player p : server.getData().getPlayers()){
			playersPanel.add(new ClientViewObj(p));
		}
		
		
		
		/*--- Update Games List ---*/
		
		gamesPanel.removeAll();
		
		gamesPanel.add(new JLabel("::: GAMES ::::"));
		
		for (Game g : server.getData().getGames()){
			this.add(new GameViewObj(g));
		}
				
		
		/*-- Update Invitations/Requests Table --*/
		
		/*
					 .addCell(new JLabel("From: " + req.getFrom()))
					 .addCell(new JLabel("To: " + req.getTo()))
				     .addCell(new JLabel("State: " + req.getState()))
				     .addCell(new JButton("Remove")));
				     */
	
		
		/*------ Update Logs -----*/
		
		logTextArea.setText("");
		
		for (int i=0; i<server.getData().getLogs().size(); i++)
			logTextArea.setText(logTextArea.getText() + server.getData().getLogs().get(i).getMsg() + "\n");
		
		revalidate();
		repaint();
		
	}
	
	private class ClientViewObj extends JPanel{
		
		/**
		 * 
		 */
		private static final long serialVersionUID = -4395237375923571681L;
		
		@SuppressWarnings("unused")
		Player p;
		
		public ClientViewObj(Player p) {
			this.p = p;
			
			setPreferredSize(new Dimension(width/3, 30));
			setBackground(DEFAULT_BG_COLOR);
			setLayout(null);
			
			String str = "Player: " + p.getUser().getUserName();
			
			str += " [" +  playerStates.get(new Integer(p.getPlayerState())) + "]";
						
			JLabel lbl = new JLabel(str);
			this.add(lbl);
			lbl.setBounds(0,0,width/3, 30);
			
		}
		
	}
	
	private class GameViewObj extends JPanel{
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 3245632500928058553L;
		@SuppressWarnings("unused")
		Game g;
		
		public GameViewObj(Game g) {
			this.g = g;
			
			setPreferredSize(new Dimension(width/3, 30));
			setBackground(DEFAULT_BG_COLOR);
			setLayout(null);
			
			JLabel lbl =new JLabel("Game: " + g.getPlayer1().getUser().getUserName() 
		    		 + " vs " + g.getPlayer2().getUser().getUserName() + " [" + g.getGameState() + "]");

			this.add(lbl);
			
		    JButton btn  = new JButton("Terminate");
			this.add(btn);
		    
		}
		
	}
	
}
